package evolution.statistics;

import java.util.ArrayList;
import java.util.List;

import evolution.individual.Individual;

public class PopulationStatisticsRegistry {

	/**
	 * List: iterations <br/>
	 * Tables: subPopulations
	 */
	private List<PopulationStatistics[]> data = new ArrayList<>();

	public void addIterationData(PopulationStatistics[] iterationData) {
		data.add(iterationData);
	}

	public List<PopulationStatistics[]> getData() {
		return data;
	}

	/**
	 * 
	 * @return Individual[iteration][population]
	 */
	public Individual[][] getMedianIndividuals() {
		int size = data.size();
		Individual[][] median = new Individual[size][];

		for (int i = 0; i < size; i++) {
			PopulationStatistics[] iterationStatistics = data.get(i);
			int populations = iterationStatistics.length;
			median[i] = new Individual[populations];
			for (int j = 0; j < populations; j++) {
				median[i][j] = iterationStatistics[j].getMedian();
			}
		}

		return median;
	}

	/**
	 * 
	 * @return Individual[iteration][population]
	 */
	public Individual[][] getBestIndividuals() {
		int size = data.size();
		Individual[][] best = new Individual[size][];

		for (int i = 0; i < size; i++) {
			PopulationStatistics[] iterationStatistics = data.get(i);
			int populations = iterationStatistics.length;
			best[i] = new Individual[populations];
			for (int j = 0; j < populations; j++) {
				best[i][j] = iterationStatistics[j].getBest();
			}
		}
		return best;
	}

}
